package com.yuncheng.oaApi.utils;

import org.apache.commons.lang3.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yuncheng.oaApi.config.OaSmsConfig;
import com.yuncheng.oaApi.constant.OaBusinessConstant;
import com.yuncheng.oaApi.vo.ReplySmsVo;
import groovy.util.logging.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.List;

/**
 * 短信发送 工具类
 */
@Component
@Slf4j
public class OaSmsUtils {
    @Resource
    private OaSmsConfig oaSmsConfig;

    private final String extendCode = "0385";

    /**
     * 自定义发送短信
     * @param phone
     * @param msg
     */
    public String sendSmsZdy(String phone,String msg) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
        return oaSmsConfig.getSmsClient().httpSendSms(phone,"【"+ OaBusinessConstant.Dw_NAME +"】"+ msg,
                                                    "","",extendCode,null);
    }

    /**
     * 获取发送短信人的回复短信
     * @param date 回复日期
     * @return
     */
    public List<ReplySmsVo> getSendSmsMoInfo(String phone, String date){
        /** 返回结果
         * {
         "errNum": "OK",
         "errMsg": "成功",
         "data": [
         {
         "extendCode": "0310",
         "phone": "13317710721",
         "sendTime": "2022-12-22 19:44:00",
         "content": "okok"
         }]}
         */

        String mo = oaSmsConfig.getSmsClient().getMo(extendCode, date, phone);

        JSONObject jsonObject = JSONObject.parseObject(mo);
        String errNum = jsonObject.getString("errNum");
        if (StringUtils.isNotBlank(errNum) && !errNum.equals("OK")){

        }
        List<ReplySmsVo> dataList = JSONArray.parseArray(jsonObject.get("data").toString(), ReplySmsVo.class);
        /*if (CollectionUtil.isNotEmpty(dataList)){
            dataList.forEach(e-> System.out.println(e));
        }*/
        return dataList;
    }

}
